import { call, put, takeEvery } from 'redux-saga/effects';
import * as actionTypes from '@src/model/actionType';
import * as API from '@src/api/index';
import { get as _get } from 'lodash';

function* fetchChainList(action) {
  try {
    const data = yield call(API.getChainList, action.params);
    yield put({
      type: actionTypes.chainList,
      payload: {
        TotalCount: _get(data, ['TotalCount'], 0),
        GroupList: _get(data, ['GroupList'], []),
      },
    });
  } catch (err) {
    console.error(err);
  }
}

function* fetchBlockList(action) {
  try {
    const data = yield call(API.getBlockList, action.params);
    yield put({
      type: actionTypes.blockList,
      payload: {
        TotalCount: _get(data, ['TotalCount'], 0),
        GroupList: _get(data, ['GroupList'], []),
      },
    });
  } catch (err) {
    console.error(err);
  }
}

const sagas = [
  takeEvery(actionTypes.fetchChainList, fetchChainList),
  takeEvery(actionTypes.fetchBlockList, fetchBlockList),
];

export default sagas;
